'Copyright 2006 James Downey

Imports System.Xml

Public Class LinkedEntity
    Inherits Entity

    '  <!--
    '  linked-entity element - used for joining one entity to it's "parent"
    '-->
    '<ElementType name='linked-entity' content='eltOnly' model='closed' order='many'>
    '  <AttributeType name='to' dt:type='string' required='no' />
    '  <AttributeType name='from' dt:type='string' required='no' />
    '  <AttributeType name='name' dt:type='string' required='yes' />
    '  <AttributeType name='alias' dt:type='string' required='no' />
    '  <AttributeType name='link-type' dt:type='string' dt:values='natural inner outer' required='no' />
    '  <!-- -->

    '  <attribute type='name' />
    '  <attribute type='to' />
    '  <attribute type='alias' />
    '  <attribute type='link-type' />
    '  <element type='all-attributes' minOccurs='0' maxOccurs='1' />
    '  <element type='attribute' minOccurs='0' maxOccurs='*' />
    '  <element type='order' minOccurs='0' maxOccurs='*' />
    '  <element type='filter' minOccurs='0' maxOccurs='1' />
    '  <element type='linked-entity' />
    '</ElementType>

    '<link-entity 
    'name="[target entity]" 
    'from="[property on target]"
    'to="[property on source]" 
    'link-type="[inner, outer or natural]"> 

    Public LinkFrom As String 'property on target
    Public LinkTo As String 'property on source
    Public LinkAlias As String 'alias
    Public LinkType As String 'link-type

    Public Sub New(ByVal Name As String)
        MyBase.New(Name)
    End Sub

    Public Sub New(ByVal LinkedEntityNode As XmlNode)
        MyBase.New(LinkedEntityNode)
        If Not LinkedEntityNode.Attributes("to") Is Nothing Then
            Me.LinkTo = LinkedEntityNode.Attributes("to").Value
        End If
        If Not LinkedEntityNode.Attributes("from") Is Nothing Then
            Me.LinkFrom = LinkedEntityNode.Attributes("from").Value
        End If
        If Not LinkedEntityNode.Attributes("alias") Is Nothing Then
            Me.LinkAlias = LinkedEntityNode.Attributes("alias").Value
        End If
        If Not LinkedEntityNode.Attributes("link-type") Is Nothing Then
            Me.LinkTo = LinkedEntityNode.Attributes("link-type").Value
        End If
    End Sub

    Public Overrides Function ToString() As String
        Dim strLinkedEntity As String
        If Not Me.LinkAlias Is Nothing AndAlso Me.LinkAlias.Length > 0 Then
            strLinkedEntity = Me.LinkAlias
        Else
            strLinkedEntity = Me.Name
        End If
        Return strLinkedEntity
    End Function

    Public Overloads Function ToXML() As String
        Dim strXML As String
        strXML = "<link-entity name='" & Name & "' "
        If Not Me.LinkFrom Is Nothing AndAlso Me.LinkFrom.Length > 0 Then
            strXML &= "from='" & Me.LinkFrom & "' "
        End If
        If Not Me.LinkTo Is Nothing AndAlso Me.LinkTo.Length > 0 Then
            strXML &= "to='" & Me.LinkTo & "' "
        End If
        If Not Me.LinkAlias Is Nothing AndAlso Me.LinkAlias.Length > 0 Then
            strXML &= "alias='" & Me.LinkAlias & "' "
        End If
        Select Case Me.LinkType
            Case "natural"
                strXML &= "link-type='natural' "
            Case "inner"
                strXML &= "link-type='inner' "
            Case "outer"
                strXML &= "link-type='outer' "
        End Select
        strXML &= ">"

        If Me.AllAttributes Then
            strXML &= "<all-attributes />"
        End If
        If Not Me.AllAttributes And Me.NoAttributes Then
            strXML &= "<no-attrs />"
        End If
        'If Not Me.AllAttributes And Me.Attributes.Count = 0 Then
        '    strXML &= "<no-attributes />"
        'End If
        If Not AllAttributes And Not Me.NoAttributes Then
            Dim attribute As Attribute
            For Each attribute In Me.Attributes
                strXML &= attribute.ToXML
            Next
        End If

        Dim filter As Filter
        For Each filter In Me.Filters
            strXML &= filter.ToXML
        Next
        Dim order As Order
        For Each Order In Me.Orders
            strXML &= Order.ToXML
        Next
        Dim linkedentity As LinkedEntity
        For Each linkedentity In Me.LinkedEntities
            strXML &= linkedentity.ToXML
        Next
        strXML &= "</link-entity>"
        Return strXML
    End Function

End Class

